//
//  PlayDataHelper.m
//  Travel
//
//  Created by lanou3g on 15/12/8.
//  Copyright © 2015年 涂晓雨. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "PlayDataHelper.h"
#import "TXY_DateDownTools.h"
#import "PlayModel.h"
#import "CityModel.h"
#import "advObject.h"
#import "SDCycleScrollView.h"
#import "SDRefresh.h"
#import "detailModel.h"

static PlayDataHelper *play = nil;
@implementation PlayDataHelper


//单利
+(instancetype)sharePlayDataHelper{
    static PlayDataHelper *play = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken, ^{
        if (play == nil) {
            play = [[PlayDataHelper alloc]init];
        }
    });
    return play;
}

-(NSMutableArray *)CityListArray{
    if (_CityListArray == nil) {
        _CityListArray = [NSMutableArray array];
    }
    return  _CityListArray;
}


-(NSMutableArray *)TourArray{

    if (_TourArray == nil) {
        _TourArray = [NSMutableArray array];
    }
    return _TourArray;
}

-(NSMutableArray *)advArray{
    if (_advArray == nil) {
        _advArray = [NSMutableArray array];
    }
    return _advArray;
}

-(NSMutableArray *)detailArray{
    if (_detailArray == nil) {
        _detailArray = [NSMutableArray array];
    }
    return _detailArray;
}

-(NSMutableArray *)introArray{
    if (_introArray == nil) {
        _introArray = [NSMutableArray array];
    }
    return _introArray;
}
//城市的数据解析
-(void)CityListData:(UITableView *)tableView{

  [TXY_DateDownTools downloadDataWithURL:@"http://webapi.yilule.com:5580//api/TourData?scaleType=2" andMethod:@"GET" andBoady:nil andBlock:^(NSData *data) {
      NSArray *CityArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
      for (NSDictionary *dic in CityArray) {
          CityModel *model = [[CityModel alloc]init];
          [model setValuesForKeysWithDictionary:dic];
          [self.CityListArray addObject:model];
      }
      [tableView reloadData];
  }];
}


//景点数据解析
-(void)tourListData:(UITableView *)tableView  ID:(NSString *)ID pageIndex:(NSInteger)index {
      
        NSString *str = [NSString stringWithFormat:@"http://webapi.yilule.com:5580//api/TourData?lid=%@&pageSize=20&pageIndex=%ld&lat=40.030343&lng=116.343381&order=6",ID,index];
        [TXY_DateDownTools downloadDataWithURL:str andMethod:@"GET" andBoady:nil andBlock:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            NSArray *array = dic[@"List"];
            self.TourArray = nil;
            for (NSDictionary *dict in array) {
                PlayModel *play = [[PlayModel alloc]init];
                [play setValuesForKeysWithDictionary:dict];
                [self.TourArray addObject:play];
            }
            [tableView reloadData];
            if (self.setValue2) {
                self.setValue2(self.TourArray);
            }
        }];
}


//导航栏的数据解析
//http://webapi.yilule.com:5580//api/Advs?count=5&siteId=21&p=1
-(void)advData:(UITableView *)tableView{
[TXY_DateDownTools downloadDataWithURL:@"http://webapi.yilule.com:5580//api/Advs?count=5&siteId=21&p=1" andMethod:@"GET" andBoady:nil andBlock:^(NSData *data) {
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    for (NSDictionary *dic in array) {
        advObject *adv = [[advObject alloc]init];
        [adv setValuesForKeysWithDictionary:dic];
        [self.advArray addObject:adv];
    }
    
    if (self.setValue) {
        self.setValue(self.advArray);
    }
    [tableView reloadData];
}];

}

//详情界面的数据的解析
-(void)detailData:(NSInteger )tourID{
    NSString *str = [NSString stringWithFormat:@"http://webapi.yilule.com:5580//api/SingleRequest?count=6&sid=%ld",tourID];

   [TXY_DateDownTools downloadDataWithURL:str andMethod:@"GET" andBoady:nil andBlock:^(NSData *data) {
       NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error: nil];
       NSArray *array = dic[@"PhotoList"];
       
       NSDictionary *dictionary = dic[@"Ptd"];
       self.introArray = nil;
       detailModel *model1 = [[detailModel alloc]init];
       [model1 setValuesForKeysWithDictionary:dictionary];
       [self.introArray addObject:model1];
    
       self.detailArray = nil;
       for (NSDictionary *dic in array) {
           detailModel *model = [[detailModel alloc]init];
           [model setValuesForKeysWithDictionary:dic];
           [self.detailArray addObject:model];
       }
       if (self.setValue1) {
           self.setValue1(self.detailArray);
       }
      
       if (self.setValue3) {
           self.setValue3(self.introArray);
       }
   }];

}



//下拉刷新
-(void)refreshData:(UITableView *)tableView{

    SDRefreshHeaderView *header = [SDRefreshHeaderView refreshView];
    [header addToScrollView:tableView];
    __weak SDRefreshHeaderView *weakHeader = header;
   // __weak typeof(self) weakSelf = self;
    header.beginRefreshingOperation = ^{
    
        [tableView reloadData];
        [weakHeader endRefreshing];
    };


    
}


//下拉加载

-(void)loadDatatableView:(UITableView *)tableView   ID:(NSString *)ID{
    SDRefreshFooterView *footer = [SDRefreshFooterView refreshView];
    [footer addToScrollView:tableView];
    __weak SDRefreshFooterView *weakFooter = footer;
   // __weak typeof(self) weakSelf = self;
    __block NSInteger  pageIndex = 2;
    footer.beginRefreshingOperation = ^{
        NSString *str = [NSString stringWithFormat:@"http://webapi.yilule.com:5580//api/TourData?lid=%@&pageSize=20&pageIndex=%ld&lat=40.030343&lng=116.343381&order=6",ID,pageIndex++];
        
        [TXY_DateDownTools downloadDataWithURL:str andMethod:@"GET" andBoady:nil andBlock:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            NSArray *array = dic[@"List"];
            
            for (NSDictionary *dict in array) {
                PlayModel *play = [[PlayModel alloc]init];
                [play setValuesForKeysWithDictionary:dict];
                [self.TourArray addObject:play];
            }
            [tableView reloadData];
        }];
        [tableView reloadData];
        [weakFooter endRefreshing];
    
    };


}


//简介的接口  http://webapi.yilule.com:5580//api/SingleRequest?count=6&sid=112
//-(void)loadDataIntroduce:(NSInteger )ID{
//    NSString *url = [NSString stringWithFormat:@"http://webapi.yilule.com:5580//api/SingleRequest?count=6&sid=%ld",ID];
//   [TXY_DateDownTools downloadDataWithURL:url andMethod:@"GET" andBoady:nil andBlock:^(NSData *data) {
//       NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//       NSDictionary *dict = dic[@"Ptd"];
//       NSLog(@"%@",dict);
//   }];
//
//}
@end
